'use strict';
var myApp = angular.module('gkwb', [
        'ui.router',
        'ngResource',
        'ngAnimate',
        'cgBusy',
        'toaster',
        'mgcrea.ngStrap',
        'easypiechart',
        'chart.js'
    ])
    .constant('_', window._)
    .run(
        ['$rootScope', '$state', '$stateParams', '$window',
            function($rootScope, $state, $stateParams, $window) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
                $rootScope._ = window._;
            }
        ]
    );

myApp.factory('authInterceptor', ['$rootScope', '$q', '$window', '$location','Base64',function($rootScope, $q, $window, $location,Base64) {
    return {
        request: function(config) {
            config.headers = config.headers || {};
            config.timeout = 10000;
            var authdata = Base64.encode('user' + ':' + '123');
            config.headers['authorization'] = 'Basic ' + authdata;
            if ($window.sessionStorage.token) {
                config.headers.sessionId = $window.sessionStorage.token;
            }
            return config;
        },
        responseError: function(response) {
            //服务器端验证，如果没有登录，返回登陆页面
            if (response.status === 401) {
                $location.path('/login');
            } else if (response.status === 500) {
                console.log('error');
            }
            return $q.reject(response);
        }
    };
}]);

myApp.config(['$httpProvider',function($httpProvider) {
    $httpProvider.interceptors.push('authInterceptor');
    $httpProvider.defaults.cache = false;
    if (!$httpProvider.defaults.headers.get) {
      $httpProvider.defaults.headers.get = {};
    }
    // disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = '0';
}]);
